package com.meta.platform.file.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meta.platform.file.mapper.FileMapper;
import com.meta.platform.file.model.FileInfo;
import com.meta.platform.file.model.FileType;
import com.meta.platform.file.utils.FileUtil;
import org.springframework.web.multipart.MultipartFile;

//import com.central.common.model.PageResult;
import com.meta.platform.file.service.IFileService;

import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;

/**
 * AbstractIFileService 抽取类
 * 根据filetype 实例化具体oss对象
 *
 * */
@Slf4j
public abstract class AbstractIFileService extends ServiceImpl<FileMapper, FileInfo> implements IFileService {

	@Resource
	private FileMapper fileMapper;

	@Override
	public FileInfo upload(MultipartFile file  ) {

		try {

			FileInfo fileInfo = FileUtil.getFileInfo(file);
			FileInfo oldFileInfo = fileMapper.selectById(fileInfo.getId());
			if (oldFileInfo != null) {
				return oldFileInfo;
			}
			if (!fileInfo.getName().equals("blob") && !fileInfo.getName().contains(".")) {
				throw new IllegalArgumentException("缺少后缀名");
			}
			uploadFile(file, fileInfo);
			fileInfo.setSource(fileType().name());// 设置文件来源

			fileMapper.insert(fileInfo);// 将文件信息保存到数据库
			return fileInfo;
		}catch (Exception e){
			log.error("======file upload error", e);
		}

		return null;
	}

	@Override
	public FileInfo upload(byte[] file, String fileName) {

		try {
			FileInfo fileInfo = FileUtil.getDefaultFileInfo(file, fileName);
			FileInfo oldFileInfo = fileMapper.selectById(fileInfo.getId());
			if (oldFileInfo != null) {
				return oldFileInfo;
			}
			if (!fileInfo.getName().equals("blob") && !fileInfo.getName().contains(".")) {
				throw new IllegalArgumentException("缺少后缀名");
			}
			uploadFile(file, fileInfo);
			fileInfo.setSource(fileType().name());// 设置文件来源

			fileMapper.insert(fileInfo);// 将文件信息保存到数据库
			return fileInfo;
		}catch (Exception e){
			log.error("======file upload error", e);
		}

		return null;
	}

	@Override
	public List<FileInfo> findFilesByIds(List<String> ids){
		List<FileInfo> list = fileMapper.selectByIds(ids);
		return list;
	}


	/**
	 * 文件来源
	 *
	 * @return
	 */
	protected abstract FileType fileType();

	/**
	 * 上传文件
	 *
	 * @param file
	 * @param fileInfo
	 */
	protected abstract void uploadFile(MultipartFile file, FileInfo fileInfo) throws Exception;

	protected abstract void uploadFile(byte[] file, FileInfo fileInfo) throws Exception;

//	/**
//	 * 删除文件资源
//	 *
//	 * @param fileInfo
//	 * @return
//	 */
//	protected abstract boolean deleteFile(FileInfo fileInfo);

//	@Override
//	public PageResult<FileInfo> findList(Map<String, Object> params){
//		Page<FileInfo> page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"));
//        List<FileInfo> list = baseMapper.findList(page, params);
//		return PageResult.<FileInfo>builder().data(list).code(0).count(page.getTotal()).build();
//	}
}
